package org.genntii.warehousesystem.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.genntii.warehousesystem.common.Result;
import org.genntii.warehousesystem.domain.dto.ProcurementDTO;
import org.genntii.warehousesystem.domain.dto.RestockDTO;
import org.genntii.warehousesystem.domain.dto.ShipmentDTO;
import org.genntii.warehousesystem.domain.vo.*;
import org.genntii.warehousesystem.service.GoodsWarehouseService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "库存接口")
@RestController
@RequestMapping("/goodsWarehouse")
public class GoodsWarehouseController {

    @Resource
    private GoodsWarehouseService goodsWarehouseService;

    @Operation(summary = "创建库存信息",description = "库存数量默认为0")
    @Parameters({
            @Parameter(name = "goodsId",description = "货品id",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "warehouseId",description = "仓库id",required = true,in = ParameterIn.QUERY)
    })
    @PostMapping("/v1/add")
    public Result create(@RequestParam("goodsId") int goodsId, @RequestParam("warehouseId")int warehouseId){
        goodsWarehouseService.create(goodsId,warehouseId);
        return Result.success();
    }
    @Operation(summary = "显示货品库存信息")
    @Parameter(name = "goodsId",description = "货品id",required = true,in = ParameterIn.QUERY)
    @GetMapping("list")
    public Result<List<GoodsWarehouseVO>> list4user(@RequestParam("goodsId")int goodsId){
        return Result.success(goodsWarehouseService.selectByGoods(goodsId));
    }

    @Operation(summary = "通过仓库展示货品列表")
    @Parameter(name = "warehouseId",description = "仓库id",required = true,in = ParameterIn.QUERY)
    @GetMapping("selectGoods8Warehouse")
    public Result<List<SimpleGoodsVO>> listGoods4user(@RequestParam("warehouseId") int warehouseId){
        return Result.success(goodsWarehouseService.selectGoodsByWarehouse(warehouseId));
    }

    @Operation(summary = "采购",description = "添加库存，添加采购信息")
    @PostMapping("/v1/procurement")
    public Result procurement(HttpServletRequest request, @RequestBody ProcurementDTO dto){
        int operatorId = (int) request.getAttribute("id");
        goodsWarehouseService.procurement(dto.getGoodsId(),dto.getWarehouseId(),operatorId,dto.getNum(),dto.getPrice());
        return Result.success();
    }

    @Operation(summary = "调货",description = "添加调货信息，并操作对应库存数量")
    @PostMapping("/v1/restock")
    public Result restock(HttpServletRequest request, @RequestBody RestockDTO dto){
        int operatorId = (int) request.getAttribute("id");
        goodsWarehouseService.restock(dto.getGoodsId(),dto.getFromWarehouseId(),dto.getToWarehouseId(),dto.getNum(),operatorId);
        return Result.success();
    }

    @Operation(summary = "发货",description = "添加发货信息，并减少库存数量")
    @PostMapping("/v1/shipment")
    public Result shipment(HttpServletRequest request, @RequestBody ShipmentDTO dto){
        int operatorId = (int) request.getAttribute("id");
        goodsWarehouseService.shipment(dto.getOrderCode(),dto.getWarehouseId(),operatorId,dto.getGoodsId(),dto.getNum());
        return Result.success();
    }

    @Operation(summary = "分页显示采购信息")
    @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    @GetMapping("/v1/listProcurement")
    public Result<List<ProcurementVO>> listProcurement(@RequestParam("page")int page){
        return Result.success(goodsWarehouseService.listProcurement(page));
    }
    @Operation(summary = "分页显示调货信息")
    @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    @GetMapping("/v1/listRestock")
    public Result<List<RestockVO>> listRestock(@RequestParam("page")int page){
        return Result.success(goodsWarehouseService.listRestock(page));
    }

    @Operation(summary = "分页显示发货信息")
    @Parameter(name = "page",description = "页码",required = true,in = ParameterIn.QUERY)
    @GetMapping("/v1/listShipment")
    public Result<List<ShipmentVO>> listShipment(@RequestParam("page")int page){
        return Result.success(goodsWarehouseService.listShipment(page));
    }
}
